/**
 * @author 张俊明
 */

let questions = questionnaire.list;
let questionList = $("#questions-list");

document.getElementById("questionnaire-title").innerHTML = questionnaire.title;

//一个页面
let radioItemTemplate = $("#template-radio-item").innerHTML;
let checkboxItemTemplate = $("#template-checkbox-item").innerHTML;
let checkboxTemplate = $("#template-checkbox").innerHTML;
let radioTemplate = $("#template-radio").innerHTML;
let textTemplate = $("#template-text").innerHTML;

let html = "";
for (let j = 0; j < questions.length; j++) {
    let questionListHtml = "";
    let tmpHtml = "";
    if (questions[j].type === "radio") {
        for (let i = 0; i < questions[j].items.length; i++) {
            tmpHtml += radioItemTemplate.replaceAll("{radioItemID}", questions[j].items[i].id)
                .replaceAll("{id}", questions[j].id)
                .replaceAll("{content}", questions[j].items[i].content);
        }
        questionListHtml += radioTemplate.replaceAll("{title}", questions[j].title)
            .replaceAll("{id}", questions[j].id)
            .replace("{items}", tmpHtml);
    } else if (questions[j].type === "checkbox") {
        for (let i = 0; i < questions[j].items.length; i++) {
            tmpHtml += checkboxItemTemplate.replaceAll("{checkboxItemID}", questions[j].items[i].id)
                .replaceAll("{id}", questions[j].id)
                .replaceAll("{content}", questions[j].items[i].content);
        }
        questionListHtml = checkboxTemplate.replaceAll("{title}", questions[j].title)
            .replaceAll("{id}", questions[j].id)
            .replace("{items}", tmpHtml);
    } else if (questions[j].type === "text") {
        tmpHtml = textTemplate.replace("{title}", questions[j].title)
            .replaceAll("{id}", questions[j].id);
        let isNecessary = "";
        if (questions[j].necessary === true) {
            isNecessary = "necessary";
        } else {
            isNecessary = "unnecessary";
        }
        questionListHtml = tmpHtml.replaceAll("{isNecessary}", isNecessary)
            .replaceAll("{id}", questions[j].id);
    }
    html += questionListHtml;
}

document.getElementById("questions-list").innerHTML = html;

//监听选项点击
questionList.addEventListener("click", function (e) {
    // 获取事件对象
    let event = e.target;
    // 获取输入框的类型
    let type = event.getAttribute("data-type");
    // 获取问题的ID
    let questionID = event.parentElement.getAttribute("data-id");
    let questionItemID = event.getAttribute("data-id");
    // 获取问题对象
    let question = questions[questionID - 1];
    if (type === "radio") {
        for (let i = 0; i < question.items.length; i++) {
            question.items[i].tempSelected = 0;
            question.items[i].tempUnselect = 1;
        }
        question.items[questionItemID - 1].tempSelected = 1;
        question.items[questionItemID - 1].tempUnselect = 0;
    }

    if (type === "checkbox") {
        if (event.checked === true) {
            question.items[questionItemID - 1].tempSelected = 1;
            question.items[questionItemID - 1].tempUnselect = 0;
        } else {
            question.items[questionItemID - 1].tempSelected = 0;
            question.items[questionItemID - 1].tempUnselect = 1;
        }
    }
});

//监听文本框
questionList.addEventListener("keyup", function (e) {
    // 获取事件对象
    let event = e.target;
    // 获取输入框的类型
    let type = event.getAttribute("data-type");
    // 获取问题的ID
    let questionID = event.parentElement.getAttribute("data-id");
    // 获取问题对象
    let question = questions[questionID - 1];

    if (type === "text") {
        if (event.value.replaceAll(" ", "") !== "") {
            question.content = event.value.trim(); //去左右的空格,".value"指输入的内容
            question.sumOfWords = event.value.trim().length;
        }
    }
});


//提交后的操作
function submitQuestions() {
    for (let i = 0; i < questions.length; i++) {

        if (questions[i].type === "radio" || questions[i].type === "checkbox") {
            for (let l = 0; l < questions[i].items.length; l++) {
                questions[i].items[l].selected += questions[i].items[l].tempSelected;
                questions[i].items[l].unselected += questions[i].items[l].tempUnselect;
                questions[i].items[l].tempSelected = 0;
                questions[i].items[l].tempUnselect = 1;
            }
        } else {
            questions[i].sumOfWordsList.push(questions[i].sumOfWords); //把数据存进列表sumOfWordsList
            questions[i].sumOfWords = 0; //初始化
            questions[i].content = "";
        }
    }
    saveDataToLocal(questionnaires, "questionnaires");
}

//点击提交
$("#btn-submit").onclick = function () {
    let submit = false; //true已填完，false则没有
    //遍历每个问题答案
    let k = 0;
    for (let i = 0; i < questions.length; i++) {
        if (questions[i].type === "radio" || questions[i].type === "checkbox") {
            for (let j = 0; j < questions[i].items.length; j++) {
                if (questions[i].items[j].tempSelected === 1 && questions[i].items[j].tempUnselect === 0) {
                    k++;
                    break;
                }
            }
        } else if (questions[i].type === "text") {
            if (questions[i].necessary === true) {
                if (questions[i].content !== "") {
                    k++;
                }
            } else if (questions[i].necessary === false) {
                k++;
            }
        }
    }
    if (k === questions.length) {
        submit = true;
    }
    if (submit === false) {
        message("您尚未完成问卷！");
        return;
    }
    myConfirm("确认提交问卷？", function () {
        submitQuestions();
        let option = document.getElementsByClassName("option");
        for (let i = 0; i < option.length; i++) {
            option[i].checked = false;
        }
        let questionTextInput = document.getElementsByClassName("question-textInput");
        for (let j = 0; j < questionTextInput.length; j++) {
            questionTextInput[j].value = "";
        }
        message("提交成功！");
        setTimeout(function () {
            location.href = "index.html";
        }, 1000);

    });
}
